<script setup>
import PageContainer from '@/components/PageContainer.vue'
import { ref } from 'vue'
import { userUpdatePwdService } from '@/api/user'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'

// 表单数据
const pwdForm = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})

// 表单引用
const pwdFormRef = ref()

// 校验规则
// 新密码不能与原密码相同
const validateNewPwd = (rule, value, callback) => {
  if (value === pwdForm.value.old_pwd) {
    callback(new Error('新密码不能与原密码相同'))
  } else {
    callback()
  }
}
// 两次输入的新密码不一致
const validateRePwd = (rule, value, callback) => {
  if (value !== pwdForm.value.new_pwd) {
    callback(new Error('两次输入的新密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在6到15个字符', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在6到15个字符', trigger: 'blur' },
    { validator: validateNewPwd, trigger: 'blur' }
  ],
  re_pwd: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: validateRePwd, trigger: 'blur' }
  ]
}

const userStore = useUserStore()
const router = useRouter()
// 提交表单
const submitForm = async () => {
  // pwdFormRef.value.validate((valid) => {
  //   if (valid) {
  //     ElMessage.success('密码修改成功！')
  //     console.log('提交数据:', pwdForm.value)
  //     // 这里可以添加实际的提交逻辑
  //     // await updatePassword(pwdForm.value)
  //   } else {
  //     ElMessage.error('请检查表单填写是否正确')
  //     return false
  //   }
  // })
  console.log('提交')
  await pwdFormRef.value.validate()
  await userUpdatePwdService(pwdForm.value)
  // console.log(res)
  ElMessage.success('密码修改成功')
  // resetForm()

  // 密码修改成功后，要退出重新登录
  // 1. 清除本地存储的 token 和 个人信息
  userStore.removeToken()
  userStore.setUser({})
  // 2. 拦截到登录页面
  router.push('/login')
}

// 重置表单
const resetForm = () => {
  pwdFormRef.value.resetFields()
}
</script>

<template>
  <PageContainer title="重置密码">
    <el-row>
      <el-col :span="12">
        <el-form
          ref="pwdFormRef"
          :model="pwdForm"
          :rules="rules"
          label-width="100px"
          class="password-form"
        >
          <!-- 1. 原密码 -->
          <el-form-item label="原密码" prop="old_pwd">
            <el-input
              v-model="pwdForm.old_pwd"
              type="password"
              show-password
              placeholder="请输入原密码"
            />
          </el-form-item>

          <!-- 2. 新密码 -->
          <el-form-item label="新密码" prop="new_pwd">
            <el-input
              v-model="pwdForm.new_pwd"
              type="password"
              show-password
              placeholder="请输入新密码"
            />
          </el-form-item>

          <!-- 3. 确认密码 -->
          <el-form-item label="确认密码" prop="re_pwd">
            <el-input
              v-model="pwdForm.re_pwd"
              type="password"
              show-password
              placeholder="请再次输入新密码"
            />
          </el-form-item>

          <!-- 4. 按钮组 -->
          <el-form-item>
            <el-button type="primary" @click="submitForm">修改密码</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </PageContainer>
</template>